package com.youlai.boot.modules.content.service.impl;

import com.aliyun.oss.model.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.modules.content.mapper.LzArticleTagMapper;
import com.youlai.boot.modules.content.service.LzArticleTagService;
import com.youlai.boot.modules.content.model.entity.LzArticleTag;
import com.youlai.boot.modules.content.model.form.LzArticleTagForm;
import com.youlai.boot.modules.content.model.query.LzArticleTagQuery;
import com.youlai.boot.modules.content.model.vo.LzArticleTagVO;
import com.youlai.boot.modules.content.converter.LzArticleTagConverter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 文章标签关联服务实现类
 *
 * @author lz0816
 * @since 2025-02-26 10:09
 */
@Service
@RequiredArgsConstructor
public class LzArticleTagServiceImpl extends ServiceImpl<LzArticleTagMapper, LzArticleTag> implements LzArticleTagService {

    private final LzArticleTagConverter lzArticleTagConverter;

    /**
    * 获取文章标签关联分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<LzArticleTagVO>} 文章标签关联分页列表
    */
    @Override
    public IPage<LzArticleTagVO> getLzArticleTagPage(LzArticleTagQuery queryParams) {
        Page<LzArticleTagVO> pageVO = this.baseMapper.getLzArticleTagPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取文章标签关联表单数据
     *
     * @param id 文章标签关联ID
     * @return
     */
    @Override
    public LzArticleTagForm getLzArticleTagFormData(Long id) {
        LzArticleTag entity = this.getById(id);
        return lzArticleTagConverter.toForm(entity);
    }
    
    /**
     * 新增文章标签关联
     *
     * @param formData 文章标签关联表单对象
     * @return
     */
    @Override
    public boolean saveLzArticleTag(LzArticleTagForm formData) {
        LzArticleTag entity = lzArticleTagConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新文章标签关联
     *
     * @param id   文章标签关联ID
     * @param formData 文章标签关联表单对象
     * @return
     */
    @Override
    public boolean updateLzArticleTag(Long id,LzArticleTagForm formData) {
        LzArticleTag entity = lzArticleTagConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除文章标签关联
     *
     * @param ids 文章标签关联ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteLzArticleTags(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的文章标签关联数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public  List<Long> tagListByArticleId(Long articleId) {
      return   this.baseMapper.listTagIdsArticleId(articleId);
    }

}
